package com.feiyuan.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.feiyuan.common.utils.ResponseUtils;
import com.feiyuan.modules.sys.entity.AppInfoEntity;
import com.feiyuan.modules.sys.service.AppInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.feiyuan.modules.sys.entity.AppVersionEntity;
import com.feiyuan.modules.sys.service.AppVersionService;
import com.feiyuan.common.utils.PageUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 *
 * @author Xupf
 * @email xupengfei131276@126.com
 * @date 2024-04-22 17:12:38
 */
@RestController
@RequestMapping("/admin/sys/appversion")
@Slf4j
public class AppVersionController {
    @Autowired
    private AppVersionService appVersionService;
    @Autowired
    private AppInfoService appInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:appversion:list")
    public ResponseUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = appVersionService.queryPage(params);

        return ResponseUtils.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:appversion:info")
    public ResponseUtils info(@PathVariable("id") Long id){
		AppVersionEntity appVersion = appVersionService.getById(id);
        AppInfoEntity appInfo = appInfoService.getById(appVersion.getAppInfoId());
        appVersion.setAppName(appInfo.getAppName());
        appVersion.setAppId(appInfo.getAppId());
        return ResponseUtils.ok().put("appVersion", appVersion);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:appversion:save")
    public ResponseUtils save(@RequestBody AppVersionEntity appVersion){
		appVersionService.saveAppVersion(appVersion);

        return ResponseUtils.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:appversion:update")
    public ResponseUtils update(@RequestBody AppVersionEntity appVersion){
		appVersionService.updateAppVersionById(appVersion);

        return ResponseUtils.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:appversion:delete")
    public ResponseUtils delete(@RequestBody Long[] ids){
		appVersionService.removeByIds(Arrays.asList(ids));

        return ResponseUtils.ok();
    }

    /**
     * 上传
     * @param request
     * @return
     */
    @PostMapping("/upload")
    public ResponseUtils upload(HttpServletRequest request) {
        try {
            log.info("=====================开始进行上传的操作=======================");
            MultipartRequest multipartRequest = (MultipartRequest) request;
            MultipartFile file = multipartRequest.getFiles("file").get(0);
            // 获取文件的后缀名，如果后缀名不是pdf的提示
            String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            log.info("此文件【{}】的后缀名为:{}", file.getOriginalFilename(), fileSuffix);
            if (!".apk".equals(fileSuffix)) {
                return ResponseUtils.error("请上传APK文件");
            }
            // 文件大小
            long fileSize = file.getSize() / 1024; // kb
            log.info("此文件【{}】的大小为:{} kb", file.getOriginalFilename(), fileSize);
            byte[] filebytes = file.getBytes();
//            String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().length() - 4);
            String downloadUrl = appVersionService.upload(filebytes, file.getOriginalFilename(), fileSize);
            if (StringUtils.isEmpty(downloadUrl)) {
                return ResponseUtils.error();
            }
            log.info("=====================完成进行上传的操作=======================");
            return ResponseUtils.ok().put("downloadUrl", downloadUrl);
        } catch (Exception e) {
            // 转化失败
            e.printStackTrace();
        }
        return ResponseUtils.ok();
    }

    /**
     * 查询所有应用
     * @return
     */
    @GetMapping("/queryAllApp")
    public ResponseUtils queryAllApp() {
        List<AppInfoEntity> list = appInfoService.list();
        return ResponseUtils.ok().put("apps", list);
    }

}
